package com.xiaolin.admin.controller;

import com.github.pagehelper.PageInfo;
import com.xiaolin.admin.entity.User;
import com.xiaolin.admin.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xiaolin
 * @Description 用户管理模块
 * @email xiaolin@gmail.com
 * @date 2024/4/27 11:42
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     *
     * @return
     */
    @GetMapping("/getAllUsers")
    @ApiOperation(value = "查询所有用户")
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    /**
     * 分页查询用户
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/getUserPage/{page}/{pageSize}")
    @ApiOperation(value = "分页查询用户")
    public PageInfo<User> getUserPage(@PathVariable Integer page,
                                      @PathVariable Integer pageSize) {
        return userService.getUserPage(page,pageSize);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    @ApiOperation(value = "添加用户")
    public boolean addUser(@RequestBody @Valid User user) {
        return userService.save(user);
    }

    /**
     * 查询用户
     *
     * @param id
     * @return
     */
    @GetMapping("/getUser/{id}")
    @ApiOperation(value = "查询用户")
    public User getUser(@PathVariable("id") Integer id) {
        return userService.getById(id);
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    @ApiOperation(value = "修改用户")
    public boolean updateUser(@RequestBody User user) {
        return userService.updateById(user);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteUser/{id}")
    @ApiOperation(value = "删除用户")
    public boolean deleteUser(@PathVariable("id") Integer id) {
        return userService.removeById(id);
    }
}
